package org.linlinjava.litemall.admin.job;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.beans.Constants;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.core.notify.NotifyService;
import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.service.LitemallAdminService;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品预警工作
 *
 * @author yliyun
 * @Author: stephen
 * @Date: 2020/2/11 9:51
 * @Version: 1.0
 * @Description: 检测商品数量预警
 * @date 2022/09/14
 */

@Component
public class GoodsWarningJob implements Job {
    /**
     * 日志记录器
     */
    private final Log logger = LogFactory.getLog(GoodsWarningJob.class);

    /**
     * litemall商品服务
     */
    @Autowired
    private LitemallGoodsService litemallGoodsService;
    /**
     * litemall管理服务
     */
    @Autowired
    private LitemallAdminService litemallAdminService;
    /**
     * litemall货物产品服务
     */
    @Autowired
    private LitemallGoodsProductService litemallGoodsProductService;
    /**
     * 通知服务
     */
    @Autowired
    private NotifyService notifyService;

    @Autowired
    private ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;


    //@Scheduled(cron = "0 0 0 1 * ?")
    public void checkCouponExpired() {
        logger.info("系统开启任务检查商品数量预警");

        List<LitemallGoodsProduct> products = litemallGoodsProductService.queryWarning();
        if(products.size() > 0){
            List<LitemallGoods> goodsList = litemallGoodsService.findByIds(products.stream().map(LitemallGoodsProduct::getGoodsId).collect(Collectors.toList()));
            goodsList.forEach(goods -> {
                Integer shopId = products.stream().filter(product->{
                    return goods.getId().equals(product.getGoodsId());
                }).findFirst().get().getShopId();
                if(shopId!=null){
                    List<LitemallAdmin> admins = litemallAdminService.findByShopId(shopId);
                    for (LitemallAdmin admin : admins) {
                        if (admin.getEmail() != null &&  RegexUtil.isEmailExact(admin.getEmail())) {
                            if(Arrays.asList(admin.getRoleIds()).indexOf(Constants.SHOPKEEPER_ROLE_ID) >= 0 ){
                                logger.info("商品数量预警发送邮件:"+goods.getName());
                                String content = goods.getName() + "The quantity is about to be insufficient";
                                notifyService.notifyMail("Commodity quantity warning",content, admin.getEmail());
                            }
                        }
                    }
                }

            });
        }

    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.checkCouponExpired();
        scheduledCronTaskRegistrar.scheduleJobLog(jobExecutionContext);
    }
}
